import { connect } from '../../vendors/weapp-redux.js'
import { API_wxUploadFile, API_changeUserInfo } from '../../redux/api.js'
import { fetchAvatar } from '../../redux/models/personal-info.js'

let pageConfig = {
  data: {
    avatar: '',
    phone: '',
    wordCount: 30
  },
  modifyWordCount(event) {
    const maxLength = 30
    const wordCount = maxLength - event.detail.value.length
    this.setData({
      wordCount
    })
  },
  verifyPhone() {
    wx.navigateTo({
      url: '/pages/phone-verify/phone-verify'
    })
  },
  saveInfo() {
    const data = {
      name: '测试',
      company: 'test',
      profession: 'test',
      whatup: 'test'
    }
    API_changeUserInfo(data)
    wx.switchTab({
      url: '/pages/home/home'
    })
  },
  changeAvatar() {
    const _this = this
    wx.chooseImage({
      count: 1,
      success: function (res) {
        const path = res.tempFilePaths[0]
        const cbSuccess = res => {
          console.log(res)
        }
        _this.fetchAvatar(path)
        API_wxUploadFile(path, cbSuccess)
      }
    })
  }
}

let mapStateToData = state => {
  return {
    phone: state.personal.phone || '必填',
    avatar: state.personal.avatar,
  }
}

let mapDispatchToPage = dispatch => ({
  fetchAvatar: avatar => dispatch(fetchAvatar(avatar))
})

pageConfig = connect(mapStateToData, mapDispatchToPage)(pageConfig)
Page(pageConfig)
